package com.zkx.fssb.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkx.fssb.domain.dto.FlowerDto;
import com.zkx.fssb.domain.po.Flower;
import com.zkx.fssb.exception.BaseException;
import com.zkx.fssb.mapper.FlowerMapper;
import com.zkx.fssb.result.PageQuery;
import com.zkx.fssb.service.FlowerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FlowerServiceImpl extends ServiceImpl<FlowerMapper, Flower> implements FlowerService {

    @Autowired
    private FlowerMapper flowerMapper;

    @Override
    public void customSave(FlowerDto flowerDto) {
        //查看表中是否有相同名称的鲜花数据
        LambdaQueryWrapper<Flower> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Flower::getFlowerName, flowerDto.getFlowerName());

        //如果有相同名称的鲜花数据，则新建失败
        if (flowerMapper.selectOne(wrapper) != null) {
            throw new BaseException("新建失败，鲜花名称已存在");
        } else {
            Flower flower = new Flower();
            BeanUtils.copyProperties(flowerDto, flower);
            flower.setCreateTime(LocalDateTime.now());
            //如果没有相同名称的鲜花数据，则新增数据
            flowerMapper.insert(flower);
        }

    }


    @Override
    public void customUpdate(FlowerDto flowerDto) {

        if (flowerDto.getFlowerId() == null) {

            throw new BaseException("更新失败，鲜花id不能为空");
        }

        //查看表中是否有相同名称的鲜花数据
//        LambdaQueryWrapper<Flower> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Flower::getFlowerName,flowerDto.getFlowerName());

        //如果有相同名称的鲜花数据，则更新失败


        Flower flower = new Flower();
        BeanUtils.copyProperties(flowerDto, flower);
        flower.setCreateTime(LocalDateTime.now());
        //如果没有相同名称的鲜花数据，则更新数据
        flowerMapper.updateById(flower);


    }

    @Override
    public Page customPage(PageQuery pageQuery) {
        Page page = new Page(pageQuery.getPageNo(), pageQuery.getPageSize());
        LambdaQueryWrapper<Flower> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(pageQuery.getFlowerName() != null, Flower::getFlowerName, pageQuery.getFlowerName())
                .eq(pageQuery.getFlowerStatus() != null, Flower::getFlowerStatus, pageQuery.getFlowerStatus());
        this.page(page, wrapper);
        List<Flower> records = page.getRecords();
        records = records.stream().map(item -> {
            if (item.getFlowerStock() == 0) {
                item.setFlowerStatus(0);
            }
            return item;
        }).collect(Collectors.toList());

        return page;
    }

    @Override
    public void batchDelete(List<Integer> ids) {

        flowerMapper.deleteBatchIds(ids);






    }
}
